/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package r2x;

import java.sql.*;
import java.util.LinkedList;
import java.util.ListIterator;

/**
 *
 * @author przemek
 */
public class DBManager {

    private String userName;
    private String password;
    private String url;
    private String dbName;
    private Connection conn;
    private DBConnector connector;
    private LinkedList<TabInfo> tabList;
    private int tabNumber;
    public static String DEFUSERNAME = "root";
    public static String DEFPASS = "x";
    public static String DEFURL = "jdbc:mysql://localhost/r2xtest";
    public static String DEFDBNAME = "r2xtest";

    public DBManager() {
        tabList = new LinkedList<TabInfo>();
        connector = new DBConnector();
    }

    public Boolean establishConnection() {
        setConn(connectToDB());
        if (getConn() != null) {
            return true;
        } else {
            return false;
        }
    }

    public Boolean closeConnection() {
        setConn(connector.disconnectFromDB(getConn()));
        if (getConn() == null) {
            return true;
        } else {
            return false;
        }
    }

    public Connection connectToDefaultDB() {
        conn = connector.connectToDB(DEFUSERNAME, DEFPASS, DEFURL);
        if (conn != null) {
            setUserName(DEFUSERNAME);
            setPassword(DEFPASS);
            setUrl(DEFURL);
            setDbName(DEFDBNAME);
        }
        return conn;
    }

    public Connection connectToDB() {
        conn = connector.connectToDB(userName, password, url);
        return conn;
    }


    public Boolean isConnected() {
        if (getConn() != null) {
            return true;
        } else {
            return false;
        }
    }

    public ResultSet getResultSetOfQuery(String query) {
        ResultSet rs = null;
        try {
            Statement s = getConn().createStatement();
            s.executeQuery(query);
            rs = s.getResultSet();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }


    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the url
     */
    public String getUrl() {
        return url;
    }

    /**
     * @param url the url to set
     */
    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * @return the conn
     */
    public Connection getConn() {
        return conn;
    }

    /**
     * @param conn the conn to set
     */
    public void setConn(Connection conn) {
        this.conn = conn;
    }

    /**
     * @return the connector
     */
    public DBConnector getConnector() {
        return connector;
    }

    /**
     * @param connector the connector to set
     */
    public void setConnector(DBConnector connector) {
        this.connector = connector;
    }

    /**
     * @return the dbName
     */
    public String getDbName() {
        return dbName;
    }

    /**
     * @param dbName the dbName to set
     */
    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    /**
     * @return the tabList
     */
    public LinkedList getTabList() {
        return tabList;
    }

    /**
     * @param tabList the tabList to set
     */
    public void setTabList(LinkedList tabList) {
        this.tabList = tabList;
    }

    /**
     * @return the tabNumber
     */
    public int getTabNumber() {
        return tabNumber;
    }

    /**
     * @param tabNumber the tabNumber to set
     */
    public void setTabNumber(int tabNumber) {
        this.tabNumber = tabNumber;
    }

    public TabInfo getTabInfoElementByName(String name) {
        TabInfo curr;
        for (int i = 0; i < tabList.size(); i++) {
            curr = tabList.get(i);
            if (curr.getTabName().equals(name)) {
                return curr;
            }
        }
        return null;
    }

    public String tabInfoListoToString() {
        TabInfo el;
        String s = "";
        for (int i = 0; i < tabList.size(); i++) {
            el = tabList.get(i);
            s = s.concat("\n" + i + el.toString());
        }
        return s;
    }

    public LinkedList<TabInfo> getTabsOfType(int _type) {
        LinkedList<TabInfo> resultList = new LinkedList<TabInfo>();
        ListIterator<TabInfo> it = tabList.listIterator();
        TabInfo tab;

        while (it.hasNext()) {
            tab = it.next();
            if (tab.getType() == _type) {
                resultList.add(tab);
            }
        }
        return resultList;
    }

    public void prepareForAnotherUsage(){
        tabList = new LinkedList<TabInfo>();
    }
}
